package com.transaction.demo.shop.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

import com.transaction.demo.common.util.JsonUtils;
import com.transaction.demo.common.util.Result;

/**
 * 描述：支付回调通知
 * 作者：马庆智
 * 日期：2023/5/5 13:47
 */
@Api(tags = "支付回调通知")
@RestController
@RequestMapping("/pay")
@Slf4j
public class NotifyController {

	/**
	 * 支付回调通知
	 *
	 * @param request 请求参数
	 * @return String
	 * @author 马庆智
	 * @date: 2023/5/5 13:49
	 */
	@ApiOperation("支付回调通知")
	@PostMapping("/notify")
	public Result<String> notify(HttpServletRequest request) {
		Map<String, String> params = new HashMap<>();
		Map<String, String[]> requestParams = request.getParameterMap();
		for (String name : requestParams.keySet()) {
			String[] values = requestParams.get(name);
			String valueStr = "";
			for (int i = 0; i < values.length; i++) {
				valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
			}
			//乱码解决，这段代码在出现乱码时使用。
			//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
			params.put(name, valueStr);
		}

		log.info("接收到支付回调通知的参数:{}", JsonUtils.toJson(params));
		return Result.success("success1");
	}
}
